const PubSub = {
    events: {},

    subscribe: function (event, callback) {
        if (!this.events[event]) {
            this.events[event] = [];
        }
        this.events[event].push(callback);
    },

    publish: function (event, data) {
        if (!this.events[event]) {
            return;
        }
        this.events[event].forEach(callback => callback(data));
    },

    unsubscribe: function (event, callback) {
        if (!this.events[event]) {
            return;
        }
        this.events[event] = this.events[event].filter(cb => cb !== callback);
    }
};

// 示例用法
function handleData(data) {
    console.log('Data received:', data);
}

PubSub.subscribe('dataReceived', handleData);

PubSub.publish('dataReceived', { message: 'Hello, world!' });

PubSub.unsubscribe('dataReceived', handleData);

PubSub.publish('dataReceived', { message: 'This will not be received' });
